import 'package:flutter/material.dart';

class TextFieldAndFormDemo extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TextFieldAndFormDemoState();
  }
}

class TextFieldAndFormDemoState extends State<TextFieldAndFormDemo>{

  TextEditingController _userNameController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    ///通过 controller 设置监听实现获取内容
    _userNameController.addListener((){

      print("通过Controller获取内容：" + _userNameController.text);
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(

      appBar: AppBar(title: Text('TextField And Form'),),

      body: Column(

        children: <Widget>[

          ///controller
          TextField(

            onChanged: (text){

              print('通过onChange获取内容：$text');
            },

            autofocus: true,

            controller: _userNameController,

            decoration: InputDecoration(

              labelText: '用户名',
              hintText: '请输入用户名',
              prefixIcon: Icon(Icons.person)

            ),
          ),

          TextField(

            decoration: InputDecoration(

              labelText: '密码',
              hintText: '请输入密码',
              prefixIcon: Icon(Icons.lock),
            ),

            //隐藏密码
            obscureText: true,
          )
        ],
      ),

    );
  }
}